import React, { Component } from 'react';
import { BsXCircleFill } from 'react-icons/bs';
import { Error } from '@/components';

interface stateProps {
  flag: boolean;
  info: string;
}

class ErrorBoundary extends Component<{}, stateProps> {
  constructor(props: any) {
    super(props);
    this.state = {
      flag: false,
      info: '',
    };
  }

  static getDerivedStateFromError(error: any) {
    return {
      flag: true,
      info: error.message,
    };
  }

  render() {
    const { flag, info } = this.state;

    return (
      <>
        {flag ? (
          <Error
            img={
              <BsXCircleFill style={{ fill: '#f2526f', fontSize: '3rem' }} />
            }
            title="出错啦/(ㄒoㄒ)/~~"
            message={info}
          />
        ) : (
          this.props.children
        )}
      </>
    );
  }
}

export default ErrorBoundary;
